#!/usr/bin/python

"""Project Euler Solution 019

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
from datetime import date, timedelta

def get_answer():
    """Question:
    
    You are given the following information, but you may prefer to do some 
    research for yourself.

    * 1 Jan 1900 was a Monday.
    
    * Thirty days has September,
        April, June and November.
        All the rest have thirty-one,
        Saving February alone,
        Which has twenty-eight, rain or shine.
        And on leap years, twenty-nine.
        
    * A leap year occurs on any year evenly divisible by 4, but not on a 
        century unless it is divisible by 400.

    How many Sundays fell on the first of the month during the twentieth 
    century (1 Jan 1901 to 31 Dec 2000)?
    """
    
    #Go through all the dates mentioned and count the number of sundays
    #which fall on the first of the month.
    current_date = date(2000, 12, 31)
    one_day = timedelta(1)
    
    total_sundays_on_first_day = 0 
    
    while current_date >= date(1901, 1, 1):
        if current_date.weekday() == 6 and current_date.strftime('%d') == '01':
            total_sundays_on_first_day += 1
            
        current_date -= one_day
    
    return total_sundays_on_first_day

if __name__ == "__main__":
    cProfile.run("print(get_answer())")
